/*
 * Copyright (c) 2006-2020, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-03-10     17481       the first version
 */
/*
 * Copyright (c) 2006-2020, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-03-08     17481       the first version
 */
#define LOG_TAG     "RTC"
#include <COMMON_HEADER/common_header.h>
struct  dtu_time dtu_times = {0,0,0,0,0,0};

/**
 * 该函数用来设置RTC时间与日期。注意参数 bkup_vaule值，每次如果要对RTC设置时间，需要重新输入参数，且参数值与之前的不同。
 * 如：第一次 bkup_vaule=0x0A0A，那么你要再次修改时间的话，需要把 bkup_vaule的值变成非0x0A0A即可
 * @param year
 * @param month
 * @param day
 * @param hour
 * @param minute
 * @param second
 * @param bkup_vaule
 */


void  set_time_bkup( rt_uint32_t year,rt_uint32_t month, rt_uint32_t day,
                   rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second,rt_uint16_t bkup_vaule)
{

    RTC_HandleTypeDef RTC_Handler;

    if(HAL_RTCEx_BKUPRead(&RTC_Handler,RTC_BKP_DR1)!=bkup_vaule)
    {
         //设置日期
         set_time(hour, minute, second);
         // 设置时间
         set_date(year, month, day);
         HAL_RTCEx_BKUPWrite(&RTC_Handler,RTC_BKP_DR1,bkup_vaule);
         LOG_I("设置RTC时间成功");
    }
    dtu_time_update();
}
/**
 * 获取当前DTU时间
 */
void  dtu_time_update()
{
     now = time(RT_NULL);
     now_t = localtime(&now);
     //更新实时时间
     dtu_times.year = (now_t->tm_year+1900);
     dtu_times.month = (now_t->tm_mon+1);
     dtu_times.day  = now_t->tm_mday;
     dtu_times.hour = now_t->tm_hour;
     dtu_times.minute = now_t->tm_min;
     dtu_times.second = now_t->tm_sec;
}





